package com.wms.controller;

import com.wms.bean.PageBean;
import com.wms.bean.ResultBean;
import com.wms.core.R;
import com.wms.dto.AllocateSaveVo;
import com.wms.dto.AllocatedetailVo;
import com.wms.dto.PermissionVo;
import com.wms.dto.SuserUpVo;
import com.wms.entity.Allocate;
import com.wms.entity.Suser;
import com.wms.service.AllocateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/allocate")
@Api(tags = "调度业务--张开心")
@Slf4j
public class AllocateController {
    @Autowired
    AllocateService allocateService;


    @PostMapping("/query")
    @ApiOperation(value = "条件以模糊及所有查询调度单")
    public ResultBean<PageBean<List<Allocate>>> query(
            @ApiParam(value = "库管员更新信息", required = true) @RequestBody AllocatedetailVo allocatedetailVo
    ){
        log.info("初始查询库管理员列表");
        PageBean<List<Allocate>> pageBean = allocateService.query(allocatedetailVo);
        return pageBean.getList().size()!=0?R.success("查询成功", pageBean):R.fail("查询失败");
    }


    @PutMapping("/updateDelete/{id}")
    @ApiOperation(value = "逻辑删除调度单")
    public ResultBean<Integer> updateStatus(
            @ApiParam(value = "要删除的调度单号ID", required = true) @PathVariable("id") int id) {
        log.info("逻辑删除调度单: {}", id);
        int rows = allocateService.remove(id);
        return rows==1?R.success("状态更新成功", rows):R.fail("删除失败");
    }

    @PutMapping("/delete/{id}")
    @ApiOperation(value = "物理删除调度单")
    public ResultBean<Integer> delete(
            @ApiParam(value = "要删除的调度单号ID", required = true) @PathVariable("id") int id) {
        log.info("物理删除调度单: {}", id);
        int rows = allocateService.removePhysics(id);
        return rows==1?R.success("状态更新成功", rows):R.fail("删除失败");
    }



    @PutMapping("/update")
    @ApiOperation(value = "更新调度单")
    public ResultBean<Integer> delete(
            @ApiParam(value = "更新调度单", required = true)@RequestBody AllocateSaveVo allocateSaveVo) {
        log.info("更新调度单: {}", allocateSaveVo);
        int rows = allocateService.updateAllocateById(allocateSaveVo);
        return rows==1?R.success("状态更新成功", rows):R.fail("删除失败");
    }

    //----------------保存

    @PostMapping("/save")
    @ApiOperation(value = "新增调度单")
    public ResultBean<Integer> insert(
            @ApiParam(value = "新增调度单", required = true) @RequestBody AllocateSaveVo allocateSaveVo) {
        log.info("新增调度单: {}", allocateSaveVo);
        int rows = allocateService.save(allocateSaveVo);
        return rows==1?R.success("新增成功", rows):R.fail("新增失败");
    }


    @GetMapping("/queryWarehouse")
    @ApiOperation(value = "查询所有仓库名")
    public ResultBean<Integer> selectWarehouse() {
        log.info("查询所有仓库名: {}");
        List<String> list = allocateService.selectWarehouse();
        return list.size()!=0?R.success("查询成功",list):R.fail("查询失败");
    }
}
